const counterService = require('../service/counterService')
const code = require('../common/code')
const auth = require('../middleware/auth')
const KoaRouter = require('koa-router')
const qs = require('qs')
const router = new KoaRouter()
//专柜磅秤租用列表
router.get('/counterList', auth(['EDP', 'AD']), async ctx => {
    ctx.body = code.getCode(code.SUCCESS, null, await counterService.getCountAndAll(ctx.session.user.store_id, qs.parse(ctx.query)))
})
//专柜磅秤租用详细信息
router.get('/counter/:id', auth(['EDP', 'AD']), async ctx => {
    ctx.body = code.getCode(code.SUCCESS, null, await counterService.getById(ctx.params.id, ctx.session.user.store_id))
})
//新增专柜磅秤租用信息
router.post('/counter', auth(['AD']), async ctx => {
    const counter = ctx.request.body
    counter.store_id = ctx.session.user.store_id
    counter.status = 1
    ctx.body = code.getCode(code.SUCCESS, null, await counterService.insert(counter))
})
//修改专柜磅秤租用信息
router.put('/counter/:id', auth(['EDP', 'AD']), async ctx => {
    const oldCounter = await counterService.getByPk(ctx.params.id, ctx.session.user.store_id)
    const counter = ctx.request.body
    let fields = []
    if (ctx.session.user.role === 'AD' && oldCounter.status === 1) {
        const begin = new Date(counter.begin)
        begin.setMonth(begin.getMonth() + counter.month)
        counter.end = begin
        fields = ['name', 'no', 'begin', 'end']
    } else if (ctx.session.user.role === 'EDP' && oldCounter.status === 2) {
        fields = ['name', 'no', 'status', 'device_id']
    } else {
        ctx.throw(403, '状态或权限错误')
    }
    await counterService.update(ctx.params.id, ctx.session.user.store_id, counter, fields)
    ctx.body = code.getCode(code.SUCCESS)
})
//专柜磅秤续租
// router.put('/counterRenewal/:id', auth(['AD']), async ctx => {
//     await counterService.renewal(ctx.params.id, ctx.session.user.store_id, ctx.request.body.month)
//     ctx.body = code.getCode(code.SUCCESS)
// })
//取消专柜磅秤租用申请,只能在状态1执行
router.delete('/counter/:id', auth(['AD']), async ctx => {
    const oldCounter = await counterService.getByPk(ctx.params.id, ctx.session.user.store_id)
    if (oldCounter.status === 1) {
        await counterService.deleteById(ctx.params.id)
        ctx.body = code.getCode(code.SUCCESS)
    } else {
        ctx.body = code.getCode(code.VALIDATE_ERROR, '状态错误')
    }
})
//分配磅秤给租用厂商,信息变为状态2
router.put('/counterDistribute/:id', auth(['EDP']), async ctx => {
    const oldCounter = await counterService.getByPk(ctx.params.id, ctx.session.user.store_id)
    if (oldCounter.status === 1) {
        ctx.body = code.getCode(
            code.SUCCESS,
            null,
            await counterService.distribute(ctx.params.id, ctx.session.user.store_id, ctx.request.body.device_id)
        )
    } else {
        ctx.body = code.getCode(code.VALIDATE_ERROR, '状态错误')
    }
})
//专柜退场,状态变为9
router.put('/counterExit/:id', auth(['EDP']), async ctx => {
    const oldCounter = await counterService.getByPk(ctx.params.id, ctx.session.user.store_id)
    if (oldCounter.status === 2) {
        ctx.body = code.getCode(code.SUCCESS, null, await counterService.exit(ctx.params.id, ctx.session.user.store_id, ctx.request.body.remark))
    } else {
        ctx.body = code.getCode(code.VALIDATE_ERROR, '状态错误')
    }
})

module.exports = router